//
//  XQBaseIdentityLocationPopView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/11.
//

import UIKit

class XQBaseIdentityLocationPopView: UIView {
    
    var selectProvinceIdx : Int = 0
    var selectCityIdx : Int = 0
    var selectDistrictIdx : Int = 0
    
    var sureHandle: ((String) -> Void)?
    
    var cancelHandle: (() -> Void)?
    
    public var selectLocation : String = "" {
        didSet {
            dealWithLocationData()
        }
    }
        
    private lazy var contentView : UIView = {
        let v = UIView()
        return v
    }()
    
    private lazy var bgImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pop_base_bg")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    private lazy var sureBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("OK", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .mainTextColor
        btn.layer.cornerRadius = 24
        btn.addTarget(self, action: #selector(sureClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var calcelBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Close", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.layer.cornerRadius = 24
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.white.cgColor
        btn.addTarget(self, action: #selector(cancelClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var titleLb : UILabel = {
        let lb = UILabel()
        lb.text = "Please select"
        lb.textColor = .white
        lb.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        return lb
    }()
    
    private lazy var locationView : UIView = {
        let view = UIView()
        return view
    }()
    
    private lazy var provinceView : XQIdentityLocationView = {
        let provinceView = XQIdentityLocationView()
        provinceView.selectLocationHandle = {[weak self] index in
            guard let srongify = self else {return}
            srongify.updateCityWithPrivinceIdx(privinceIdx: index)
        }
        return provinceView
    }()
    
    private lazy var cityView : XQIdentityLocationView = {
        let cityView = XQIdentityLocationView()
        cityView.selectLocationHandle = {[weak self] index in
            guard let srongify = self else {return}
            srongify.updateDistrictWithCityIdx(cityIdx: index)
        }
        return cityView
    }()
    
    private lazy var districtView : XQIdentityLocationView = {
        let districtView = XQIdentityLocationView()
        districtView.selectLocationHandle = {[weak self] index in
            guard let srongify = self else {return}
            srongify.selectDistrictIdx = index
        }
        return districtView
    }()
    
    private lazy var middleView : UIView = {
        let view = UIView()
        view.backgroundColor = .mainTextColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        makeUI()
        makeConstraints()
        initData()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    private func makeUI() {
        self.addSubview(self.contentView)
        self.contentView.addSubview(self.bgImageView)
        self.contentView.addSubview(self.sureBtn)
        self.contentView.addSubview(self.calcelBtn)
        self.bgImageView.addSubview(self.titleLb)
        self.bgImageView.addSubview(self.middleView)
        self.bgImageView.addSubview(self.locationView)
        self.locationView.addSubview(self.provinceView)
        self.locationView.addSubview(self.cityView)
        self.locationView.addSubview(self.districtView)
    }
    
    private func makeConstraints() {
        let width = (UIDevice.xq_width() - 32) / 3
        
        self.contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        self.bgImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.width.equalTo(UIDevice.xq_width() - 32)
            make.height.equalTo(420)
        }
        self.sureBtn.snp.makeConstraints { make in
            make.top.equalTo(self.bgImageView.snp.bottom).offset(30)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(48)
        }
        self.calcelBtn.snp.makeConstraints { make in
            make.top.equalTo(self.sureBtn.snp.bottom).offset(15)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(48)
            make.bottom.equalTo(0)
        }
        self.titleLb.snp.makeConstraints { make in
            make.top.equalTo(65)
            make.left.equalTo(24)
            make.height.equalTo(16)
        }
        self.locationView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLb.snp.bottom).offset(24)
            make.left.right.equalTo(0)
            make.width.equalTo(UIDevice.xq_width() - 32)
            make.height.equalTo(6 * 46)
        }
        self.provinceView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.width.equalTo(width)
            make.bottom.equalTo(0)
        }
        self.districtView.snp.makeConstraints { make in
            make.top.equalTo(self.provinceView.snp.top)
            make.bottom.equalTo(self.provinceView.snp.bottom)
            make.right.equalTo(0)
            make.width.equalTo(width)
        }
        self.cityView.snp.makeConstraints { make in
            make.top.equalTo(self.provinceView.snp.top)
            make.left.equalTo(self.provinceView.snp.right)
            make.right.equalTo(self.districtView.snp.left)
            make.bottom.equalTo(self.provinceView.snp.bottom)
        }
        self.middleView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLb.snp.bottom).offset(25 + 46 * 2)
            make.left.right.equalTo(0)
            make.height.equalTo(46)
        }
    }
    
    private func initData() {
        self.provinceView.dataSource = XQCityManager.shared.getProvinceList()
        self.cityView.dataSource = XQCityManager.shared.getCityList(index: self.selectProvinceIdx)
        self.districtView.dataSource = XQCityManager.shared.getDistrictList(provinceIdx: self.selectProvinceIdx, cityIndex: self.selectCityIdx)
    }
}

extension XQBaseIdentityLocationPopView {
    
    private func dealWithLocationData() {
        
        let datas = self.selectLocation.components(separatedBy: "|")
        
        if (datas.count != 3) {return}
        
        let province = datas[0]
        let city = datas[1]
        let district = datas[2]
        
        updateProvince(province: province,cityName: city,districtName: district)
    }
    
    private func updateProvince(province : String,cityName : String,districtName : String) {
        let pIdx : Int = XQCityManager.shared.provinceIdx(pro: province)
        let pList : [String] = XQCityManager.shared.getProvinceList()
        self.provinceView.selectLocation = pList[pIdx]
        
        updateCityWithPrivinceIdx(privinceIdx: pIdx)
        
        updateCity(provinceIdx: pIdx,cityName: cityName,districtName: districtName)
    }
    
    private func updateCity(provinceIdx : Int,cityName : String,districtName : String) {
        let cIdx = XQCityManager.shared.cityIdx(provinceIdx: provinceIdx, cityName: cityName)
        let cList = XQCityManager.shared.getCityList(index: provinceIdx)
        self.cityView.selectLocation = cList[cIdx]
        
        updateDistrictWithCityIdx(cityIdx: cIdx)
        
        updateDistrict(provinceIdx: provinceIdx, cityIdx: cIdx, districtName: districtName)
    }
    
    private func updateDistrict(provinceIdx : Int,cityIdx : Int,districtName : String) {
        let dIdx = XQCityManager.shared.districtIdx(provinceIdx: provinceIdx, cityIndex: cityIdx, districtName: districtName)
        let dList = XQCityManager.shared.getDistrictList(provinceIdx: provinceIdx, cityIndex: cityIdx)
        selectDistrictIdx = dIdx
        self.districtView.selectLocation = dList[dIdx]
    }
    
    private func updateCityWithPrivinceIdx(privinceIdx : Int) {
        selectProvinceIdx = privinceIdx
        selectCityIdx = 0
        selectDistrictIdx = 0
        self.cityView.dataSource = XQCityManager.shared.getCityList(index: privinceIdx)
        self.districtView.dataSource = XQCityManager.shared.getDistrictList(provinceIdx: privinceIdx, cityIndex: 0)
    }
    
    private func updateDistrictWithCityIdx(cityIdx : Int) {
        selectCityIdx = cityIdx
        selectDistrictIdx = 0
        self.districtView.dataSource = XQCityManager.shared.getDistrictList(provinceIdx: self.selectProvinceIdx, cityIndex: cityIdx)
    }
}

extension XQBaseIdentityLocationPopView {
    
    @objc private func sureClick() {
        let p = self.provinceView.dataSource[selectProvinceIdx]
        let c = self.cityView.dataSource[selectCityIdx]
        let d = self.districtView.dataSource[selectDistrictIdx]
        let date = "\(p)|\(c)|\(d)"
        sureHandle?(date)
    }
    
    @objc private func cancelClick() {
        cancelHandle?()
    }
}
